<template>
  <search-table
      :data="vulnerabilityList"
      :columns="columns"
      :page.sync="queryParams.pageIndex"
      :size.sync="queryParams.pageSize"
      :total="vulnerabilityTotal"
      :loading="vulnerabilityLoading"
      @queryFun="getNewData">
    <template slot="search">
      <Button class="header-btn" type="primary" ghost icon="ios-add" style="height: 32px;margin-left: 5px;" @click="$refs.submitBug.showModal()">
        创建缺陷
      </Button>
      <Select filterable clearable v-model="queryParams.level" @on-change="selectLevel" placeholder="缺陷评级" style="margin-left: 15px;width: 150px">
        <Option v-for="industry in LEVEL_TYPE_OPTIONS" :key="industry.value" :value="industry.value">{{industry.label}}</Option>
      </Select>
      <Input v-model.trim="queryParams.name"  clearable placeholder="缺陷名称" @on-change="searchName" style="width: 180px;margin-left: 15px" />
      <Button class="header-btn" type="primary" ghost style="height: 32px;margin-left: 5px;" @click="refreshList()">
        <i class="el-icon-refresh-right" />
      </Button>
    </template>
    <Modal
        v-model="modalDeatilData.show"
        title="潜在漏洞详情"
        footer-hide
        class="my-modal">
      <div class="item-table">
        <p>
          <span class='label'>缺陷名称</span>
          <span>{{ detailInfo.name }}</span>
        </p>
        <p>
          <span class='label'>关联产品</span>
          <span>{{ detailInfo.productName }}</span>
        </p>
        <p>
          <span class='label'>所属模块</span>
          <span>{{ detailInfo.moduleName }}</span>
        </p>
        <p>
          <span class='label'>缺陷评级</span>
          <span>{{ vulnerabilityLevelEscape(detailInfo.level).text }}</span>
        </p>
        <p>
          <span class='label'>附件</span>
          <span>
              <template v-if="detailInfo.fileId">
                  <a href="javascript:;" @click="download(detailInfo)">{{ detailInfo.fileName }}</a>
              </template>
              <template v-else>
              </template>
          </span>
        </p>
        <p>
          <span class='label'>发现时间</span>
          <span>{{ detailInfo.created }}</span>
        </p>
        <p>
          <span class='label'>创建者</span>
          <span>{{ detailInfo.creatorName }}</span>
        </p>
      </div>
    </Modal>
    <submit-bug-vulnerability 
      ref="submitBug" 
      @update-succee="getNewData"
      @add-succee="refreshList"
      :module="module"
      :productId="productId"></submit-bug-vulnerability>
  </search-table>
</template>
<script>

import {getTestDiscoveredVulnerabilitiesPage} from '../api/testTwoRestart.js'


import AddLeakManage from "./addLeakManageFormExper.vue";
import SubmitBugVulnerability from "@/view/testPlatform/components/submit-bug-vulnerability/index.vue";
import {leakTableCol} from "../configData/leakTableCol";
//import {getAllTestObjects,getAllTestItems,getALLTestCase,getAllTestGroup,getAllNetwork} from '@/view/testServe/testProject/mixLogic/testProjectData.js'
import {mapState} from "vuex";
import {deleteProductVulnerability, queryVulnerabilityListNew} from "../../vulnerability/api/vulnerability";
import {vulnerabilityLevelEscape} from "../../vulnerability/logic/vulnerabilityLogic";
import {productPartTypeEscape} from "../logic/assetLogic";
import {deleteProductComponentLink} from "../api/productComponentLink";
import {deleteBug, queryBugList} from "../api/testBug";
import {testBugCol} from "../configData/testBugCol";
import {LEVEL_TYPE_OPTIONS} from '../../paramsConfig'
export default {
  name:'leakManageDo',
  components: {AddLeakManage, SubmitBugVulnerability},
  props:{
    module: {
      type: String,
      default: ''
    },
    productId:{
      type:String,
      default:''
    }
  },
  data(){
    return{
      vulnerabilityLevelEscape,
      productPartTypeEscape,

      modalDeatilData:{
        show:false
      },
      detailInfo:{},
      LEVEL_TYPE_OPTIONS,
      vulnerabilityList:[],
      queryParams:{
        pageIndex:1,
        pageSize:10
      },
      vulnerabilityTotal:0,
      vulnerabilityLoading:false,
      networkMeshList:[],
      targetList:[],
      testCaseList:[],
      testItemList:[],
      userGroup:[],
      showLoading:true,
      columns:[],
      isMountedLoad:false,
      features:[],
      textFields: [
        {
          label: '漏洞评级',
          key: 'level',
          placeholder:'请选择漏洞评级',
          type: 'select',
          options: [],
          slotName: null,
          hasButton: false, // 在这个示例中，不需要在性别字段后面添加按钮
          hasButton1:false
        },
        {
          label: '处置优先级',
          key: 'disposalPriority',
          type: 'select',
          placeholder:'请选择处置优先级',
          options:[],
          slotName: null,
          hasButton: false, // 在这个示例中，不需要在性别字段后面添加按钮
          hasButton1:false
        },
        {
          label: '漏洞状态',
          key: 'status',
          type: 'select',
          options: [],
          slotName: null,
          hasButton: false, // 在这个示例中，不需要在性别字段后面添加按钮
          hasButton1:false
        },
      ],
      formData:{

      },
      fieldChangeCallbacks:{
        level:this.queryList,
        status:this.queryList
      },
      formStyle:{
        direction:'horizon',
        left:true
      },
      textFields1: [
        {
          label: '',
          key: 'type',
          type: 'button',
          placeholder:'创建漏洞',
          slotName: null,
          hasButton: false, // 在这个示例中，不需要在性别字段后面添加按钮
          hasButton1:false
        }
      ],


      opList:[
        {
          txt: '详情',
          fun: (obj) => {
            this.detailInfo = obj
            this.modalDeatilData.show = true
          }
        },
        {
          txt: '修改',
          fun: (obj) => {
            this.$refs.submitBug.showModal('', '', obj)
          }
        },
        // {
        //   txt: '日志',
        //   fun:(obj)=>{
        //
        //   }
        // },
        {
          txt: '删除',
          tooltip: true,
          title: '确定要删除吗？',
          fun:(obj)=>{
            this.deleteItem(obj.id);
          }
        },
        // {
        //   txt: '更多',
        //   expressions:'more',
        //   btns:[
        //     {
        //       label:'下载附件',
        //       value:'down',
        //       disabledFn:(obj)=>{
        //         return !obj.fileId;
        //       }
        //     },
        //     // {label:'关闭缺陷',value:'close'}
        //   ],
        //   fn:(name,obj)=>{
        //     switch (name){
        //       case 'down':this.downLoadAnnex(obj);break;
        //       case 'close':this.closeBug(obj);break;
        //       default:break;
        //     }
        //   }
        // }
      ],
    }
  },
  methods:{
    getNewData(){
      this.vulnerabilityLoading = true
      let params = this.queryParams;
      params.productId = this.productId||this.$route.query.productId;
      params.partId = this.$route.query.id
      queryBugList(params)
          .then(res => {
            if (res.code == 1) {
              this.vulnerabilityTotal =res.data.total;
              this.vulnerabilityList = res.data.items
            }else{
              this.$Message.error(res.message);
            }
          })
          .finally(() => {
            this.vulnerabilityLoading = false
          })
    },
    // 删除外部链接
    deleteItem(id) {
      deleteBug(id).then(({ code, message }) => {
        if (code !== 1) {
          this.$Message.warning(message)
          return
        }
        this.$Message.success(`删除成功`)
        // 删除后展示当前页，当前页没有数据展示前一页
        this.queryParams.pageIndex = (this.vulnerabilityList.length === 1 && this.queryParams.pageIndex > 1) ?
            this.queryParams.pageIndex - 1 : this.queryParams.pageIndex;
        this.getNewData();
      }).catch(() => {
        this.$Message.error(`删除异常`)
      })
    },
    selectLevel(){
      this.queryParams.pageIndex = 1;
      this.getNewData();
    },
    searchName(){
      this.queryParams.pageIndex = 1;
      this.getNewData();
    },
    refreshList(){
      this.queryParams = {
        pageIndex: 1,
        pageSize: 10
      }
      this.getNewData()
    },
    downLoadAnnex(item){
      if(!item.fileId){
        return;
      }
      this.$CommonFn.downLoadFile('ID',item.fileId,item.fileName)
    },
    download(item) {
        if (!item.fileId) {
            return
        }
        let a = document.createElement('a');
        let fileName = item.fileName; //filename名称截取
        a.href = `/api/minio/download/${item.fileId}`;
        a.download = fileName; //命名下载名称
        a.click(); //点击触发下载
    },
    closeBug(item){

    },
    init(){
      //this.getNewData();
    },
  },
  mounted() {
    this.getNewData();
  },
  created() {
    //this.initSearchFn();
    this.columns = testBugCol(this);
  },
  computed:{
    ...mapState('testDetail',['testDetailObj'])
  }

}
</script>
<style scoped>
.search{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.bug-content{
  height: 100%;
  display: flex;
  flex-direction: column;

}
.bug-content .bug-content-table{
  flex:auto;
}
.item-table {
  display: table;
  width: 100%;
  border: 1px solid #eee;
  table-layout: fixed;
  word-break: break-all;
}

.item-table p {
  display: table-row;
  line-height: 36px;
  text-align: center;
}

.item-table p span {
  display: table-cell;
  border-bottom: 1px solid #eee;
}

</style>
